<?php

namespace app\api\controller\v1;

use app\api\model\v1\Dianpu;
use app\api\model\v1\Shopuser;
use app\api\model\v1\Shopwithdraw;
use app\common\controller\Api;
use app\common\controller\Shopapi;
use app\common\model\Config;
use app\index\model\wanlshop\Goods;
use fast\Random;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 提现接口
 */
class Withdraw extends Shopapi
{
    protected $noNeedLogin =['login'] ;
    protected $noNeedRight = ['*'];
    
    public function add(){
        $shopuser = $this->shopauth->getUser();
        $money= $this->request->param('money');
        $type= $this->request->param('type');
        $alipay_number = $this->request->param('alipay_number');
        $alipay_name = $this->request->param('alipay_name');
        $mobile= $this->request->param('mobile');
        $wx_number  = $this->request->param('wx_number');
        if($type == 2){
            if(empty($alipay_name) || empty($alipay_number) || empty($mobile) || empty($money) ){
                $this->error('支付宝不全');
            }
            //验证支付宝账号正则
            if(strpos($alipay_number,'@')){
                //走邮箱正则
                if (!preg_match("/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,}$/",$alipay_number)) {
                    $this->error('支付宝账号错误');
                }
            }else{
                //走手机号
                if (!preg_match("/^1[3-9][0-9]{9}$/", $alipay_number)) {
                    $this->error('支付宝账号错误');
                }
            }
        }elseif($type == 1){
            if(empty($wx_number)  ){
                $this->error('微信号不能为空');
            }

        }

        if($money > $shopuser['money']){
            $this->error('余额不足');
        }
        // $withdraw_few = Db::name('config')->where('name','withdraw_few')->value('value');
        // $withdraw_many = Db::name('config')->where('name','withdraw_many')->value('value');
        // $withdraw_commission = Db::name('config')->where('name','withdraw_commission')->value('value');

        // if ($money < $withdraw_few) {
        //     $this->error("提现金额最少$withdraw_few");
        // }
        // if ($money > $withdraw_many) {
        //     $this->error("提现金额最多$withdraw_many");
        // }
        // $withdraw_commission_money =    bcadd($money * $withdraw_commission/100, 0, 2);
        // $sj_money =$money - $withdraw_commission_money ;
        Db::startTrans();
        try {

            if($type == 2){
                //生成提现记录
                $data = [
                    'shop_id' => $shopuser['id'],
                    'money' => $money,
                    'withdraw_money' => $money,
                    'withdraw_fee' => 0,
                    'alipay_number' => $alipay_number,
                    'alipay_name' => $alipay_name,
                    'mobile' => $mobile,
                    'type' => $type,
                    'createtime' =>time(),
                    'updatetime' =>time(),
                ];
            }elseif($type == 1){
                //生成提现记录
                $data = [
                    'shop_id' => $shopuser['id'],
                    'money' => $money,
                    'withdraw_money' => $money,
                    'withdraw_fee' => 0,
                    'type' => $type,
                    'wx_number' => $wx_number,
                    'createtime' =>time(),
                    'updatetime' =>time(),
                ];

            }

            $withdraw_id = Shopwithdraw::insertGetId($data);
            //扣余额
            Shopuser::money(1,-$money,$shopuser['id'],$withdraw_id);
            
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success("申请提现成功");

    }

    //提现记录
   public function withdraw_log()
   {
       $pagesize = $this->request->param("pagesize", 10);
       $shopuser = $this->shopauth->getUser();
       $log =Shopwithdraw::where(['shop_id' => $shopuser['id']])->order("createtime DESC")
           ->paginate($pagesize)->each(function ($row) {
               $row->updatetime = date('Y-m-d H:i:s', $row->updatetime);
           });
       $this->success("列表", $log);
   }
}